package com.hexb.smh.entity.param.product;

import com.hexb.smh.entity.Product;
import com.hexb.smh.entity.ProductPrice;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import java.util.List;
import java.util.stream.Collectors;

import static com.hexb.smh.utils.SmhConstant.AMOUNT_PATTERN;
import static com.hexb.smh.utils.Converters.stringToDecimal;

/**
 * @author : hexb
 */
@Data
public class ProductParam {


    @ApiModelProperty(value = "项目名称,全系统唯一", example = "心电图")
    @NotNull
    @Size(min = 1, max = 50)
    private String name;


    @ApiModelProperty(value = "项目编号,全系统唯一", example = "Z000001")
    @NotNull
    @Pattern(regexp = "^[a-zA-Z\\d]{1,20}$")
    private String itemNo;


    @ApiModelProperty(value = "项目简码", example = "xdt")
    @NotNull
    @Size(min = 1, max = 20)
    private String briefCode;

    @ApiModelProperty(value = "描述,业务员可见", example = "心电图")
    @Size(max = 500)
    private String description;

    @ApiModelProperty(value = "备注,仅管理员可见", example = "推荐项目")
    @Size(max = 500)
    private String remarks;

    @ApiModelProperty(value = "项目", example = "外科")
    private String category;

    @NotEmpty
    @Valid
    List<ProductPriceParam> prices;

    public Product toProduct() {
        Product product = new Product();
        BeanUtils.copyProperties(this, product, itemNo);
        product.setItemNo(itemNo.toUpperCase());
        if (CollectionUtils.isNotEmpty(prices)) {
            product.setProductPrice(prices.stream().map(ProductPriceParam::to)
                    .collect(Collectors.toList()));
        }
        return product;
    }


    static public class ProductParamInsert extends ProductParam {

    }

    static public class ProductParamUpdate extends ProductParam {

        public Product toProduct(Integer id) {
            Product product = super.toProduct();
            product.setId(id);
            return product;
        }
    }
}
